
const LOOP_NUMBER = 40000;
const TIMES_KILO_MILLISECONDS = 1000;
const BENCHMARK_COUNT = 120;
const NUM_1 = 1;
const NUM_2 = 2;
const NUM_3 = 3;
const NUM_4 = 4;
const NUM_5 = 5;
const NUM_6 = 6;
const NUM_7 = 7;
declare interface ArkTools {
  timeInUs(args: number): number;
}
function deBugLog(str: string): void {
  print(str);
}

function run(): void {
  print('Hello world!');

  const m = LOOP_NUMBER;
  let j = 0;
  let strItems:string[] = new Array();
  function addStrElement(): void {
    while (j < m) {
      strItems.push('digit${j}');
      j = j + 1;
    }
  }
  addStrElement();

  const n = LOOP_NUMBER;
  let i = 0;
  let items:number[] = new Array();
  function addElement(): void {
    while (i < n) {
      items.push(i);
      i += 1;
    }
  }
  addElement();

  function firstWhere(list: number[], fn: (x: number) => boolean): number {
    for (const x of list) {
      if (fn(x)) {
        return x;
      }
    }
    return 0;
  }

  const nums: number[] = [NUM_1, NUM_2, NUM_3, NUM_4, NUM_5, NUM_6, NUM_7];
  function isEven(x: number): boolean {
    return (x & 1) === 0;
  }
  const firstEven = firstWhere(nums, isEven);
  
}

class Benchmark {
  /*
   * @Benchmark
   */
  runIteration(): void {
    const before = ArkTools.timeInUs() / TIMES_KILO_MILLISECONDS;
    for (let i = 0; i < BENCHMARK_COUNT; i++) {
      run();
    }
    const after = ArkTools.timeInUs() / TIMES_KILO_MILLISECONDS;
    print('LuaJSFight: ms = ' + (after - before));
  }
}


let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
  let benchmark = new Benchmark();
  benchmark.runIteration();
}

ArkTools.waitAllJitCompileFinish();

let benchmark = new Benchmark();
benchmark.runIteration();






